package com.demo.steammanagement.service;

import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.model.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class AuthService {

  @Autowired
  private IUsersService usersService;

  public boolean auth(HttpServletRequest request, Authentication authentication) {
    //权限认证
    //请求路径
    String requestURI = request.getRequestURI();
    Object principal = authentication.getPrincipal();

    if (principal == null || "anonymousUser".equals(principal)) {
      //未登录
      return false;
    }
    UserDetails userDetails = (UserDetails) principal;
    String role = userDetails.getAuthorities().toArray()[0].toString();
    if ("ROLE_USER2".equals(role)) {
      return false;
    }
//    if ("ROLE_USER4".equals(role) || "ROLE_USER3".equals(role)) {
//      String username = userDetails.getUsername();
//      Users users = usersService.getUserByUsername(username);
//      // 获取用户的账户过期日期
//      LocalDateTime accountExpirationDate = users.getAccountExpirationDate();
//      // 获取当前时间
//      LocalDateTime now = LocalDateTime.now();
//      // 比较两个日期
//      if (accountExpirationDate == null || !accountExpirationDate.isAfter(now)) {
//        throw new BizException("会员已过期: " + username);
//      }
//    }
    return true;
  }


}
